package Stack;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 故我
 * Date: 2024-02-01
 * Time: 20:18
 */
public class MyStack implements IStack {

    private int[] array;
    private int useSize;

    private static final int DefaultCapacity = 10;

    public MyStack() {
        array = new int[DefaultCapacity];
    }

    @Override
    public void push(int x) {
        // 判断栈是否满了
        if (full()) {
            // 扩容
            array = Arrays.copyOf(array, 2 * array.length);
        }
        array[useSize] = x;
        useSize++;
    }

    @Override
    public int pop() {
        // 判断栈是否为空
        if (empty()) {
            throw new EmptyExption("栈为空!");
        }
        int old = array[useSize - 1];
        useSize--;
        // 如果为引用类型要置空
        return old;
    }

    @Override
    public int peek() {
        // 判断栈是否为空
        if (empty()) {
            throw new EmptyExption("栈为空!");
        }
        return array[useSize - 1];
    }

    @Override
    public int size() {
        return useSize;
    }

    @Override
    public boolean empty() {
        return useSize == 0;
    }

    @Override
    public boolean full() {
        return useSize == size();
    }
}
